package com.szholly.pro.watergas_server.ps.service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.stereotype.Service;

import com.szholly.pro.watergas_server.common.data.DataUtil;
import com.szholly.utils.util.StringUtils;
import com.szholly.data.general.GlobalParameter;
import com.szholly.data.general.RowBase;

/**
 * 传输有效率 数据查询
 * @author 熊伟
 * 2017年6月29日
 */
@Service
public class TransValidationService {
	/**
	 * 获取站点运行时间，默认站点24小时都在运行
	 * @param st 开始时间
	 * @param et 结束时间
	 * @return
	 */
	public String getStationTime(String st,String et){
		//一小时有多少毫秒
		 long nh = 1000 * 60 * 60;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date ct_date=new Date ();
		Date et_date=new Date();
		try {
			ct_date = sdf.parse(st);
			et_date = sdf.parse(et);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		// 获得两个时间的毫秒时间差异
	    long diff = et_date.getTime() - ct_date.getTime();
	    // 计算差多少小时
	    long counthour = diff / nh;
	    //如果是23:59:59就加1
	    if(et.contains("23:59:59")){
	    	counthour=counthour+1;
	    }
	    return counthour+"";
	}
	
	/**
	 * 获取站点参数
	 * @param stationId
	 * @return
	 */
	public List<RowBase> getpollParam(String stationId){
		String sql="select t.name,t1.device_num,t.site_id from BC_SITE_POLL t left join BC_COLLECTION_INFO t1 on t.site_id=t1.site_id where t.site_id='"+stationId+"'";
		List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
		List<RowBase> row = DataUtil.getSelectRows(sql, parms);
		return row;
	}
	
	/**
	 * 获取时间段内参数条数
	 * @param mn
	 * @param parmname
	 * @param st
	 * @param et
	 * @return
	 */
	public int getpollData(String mn,String parmname,String st ,String et){
		String sql="select t.*  from monitor_data_hour t where t.mn='"+mn+"' and t.parmname in ("+parmname+") and t.datatime >= to_date('"
				   +st+"','yyyy-MM-dd hh24:mi:ss') and t.datatime <= to_date('"+et+"','yyyy-MM-dd hh24:mi:ss')";
		List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
		List<RowBase> row = DataUtil.getSelectRows(sql, parms);
		return row.size();
	}

	/**
	 * 获取有效组数()
	 * @param mn
	 * @param st
	 * @param et
	 * @return
	 */
	public int getValidData(String mn,String st ,String et){
		String sql="select count(*) as sum  from (select distinct (t.mn||t.qn) as guid from monitor_data_hour t where t.flag ='N' and t.mn='"+mn+"' and t.datatime >= to_date('"
				   +st+"','yyyy-MM-dd hh24:mi:ss') and t.datatime <= to_date('"+et+"','yyyy-MM-dd hh24:mi:ss'))";
		List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
		List<RowBase> row = DataUtil.getSelectRows(sql, parms);
		int result=0;
		if(row.size()>0){
			result=row.get(0).getSafeInt("SUM");
		}
		return result;
	}
	/**
	 * 获取流量为0的数据条数
	 * @param mn
	 * @param st
	 * @param et
	 * @return
	 */
	public int getB01ZeroData(String mn,String st ,String et){
		String sql="select t.avg  from  monitor_data_hour t where t.flag ='N' and t.mn='"+mn+"' and t.datatime >= to_date('\n"
				   +st+"','yyyy-MM-dd hh24:mi:ss') and t.datatime <= to_date('"+et+"','yyyy-MM-dd hh24:mi:ss') \n"+
				   " and t.avg=0 and t.parmname='B01TOTAL' \n";
		List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
		List<RowBase> row = DataUtil.getSelectRows(sql, parms);
		int result=0;
		if(row.size()>0){
			result=row.size();
		}
		return result;
	}
	
	/***
	 * 根据故障时间计算缺失数据组数
	 * @param mn
	 * @param st
	 * @param et
	 * @return
	 * @throws ParseException 
	 */
	public int getHoursDistance(String errorStartTime,String errorEndTime) throws ParseException{
		int hours=0;
		if(StringUtils.IsNullOrEmpty(errorStartTime) ||StringUtils.IsNullOrEmpty(errorEndTime)) {
			return hours;
		}
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date start = dateFormat.parse(errorStartTime);
		Date end =dateFormat.parse(errorEndTime);
		if(start.after(end)) {
			return hours;
		}
		long StartMills = start.getTime();
		long EndMills = end.getTime();
		long startHours = StartMills/(3600*1000);
		long endHours = EndMills/(3600*1000);
		long millsDistance = EndMills-StartMills;
		if(millsDistance>0 && millsDistance<=3600000) {
			hours=1;
		} else {
			hours = (int)Math.ceil(endHours-startHours);
		}
		
		return hours;
	}
	
	/**
	 * 获取修约后未上传凭证的数据组数
	 * @param mn
	 * @param st
	 * @param et
	 * @return
	 */
	public int getUnLoadCertificatePacketNum(String mn,String st ,String et){
		String sql="select t.datatime  from  monitor_data_hour t where t.flag ='N' and t.mn='"+mn+"' and t.datatime >= to_date('\n"
				   +st+"','yyyy-MM-dd hh24:mi:ss') and t.datatime <= to_date('"+et+"','yyyy-MM-dd hh24:mi:ss') \n"+
				   " and t.data_from>0 and t.modify_certificate_id is null \n"+
				   " and t.modify_date is not null \n"+
				   " group by t.datatime ";
		List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
		List<RowBase> row = DataUtil.getSelectRows(sql, parms);
		int result=0;
		if(row.size()>0){
			result=row.size();
		}
		return result;
	}
	
	/**
	 * 获取站点参数(获取污染源所有站点ID)
	 * @param stationId
	 * @return
	 */
	public List<RowBase> getStationId(){
		String sql="select t.code from BC_SITE_INFO t  where t.station_category in ('2','4') and t.code in ('HOLYAIR0000120')";
		List<GlobalParameter> parms = new ArrayList<GlobalParameter>();
		List<RowBase> row = DataUtil.getSelectRows(sql, parms);
		return row;
	}
	
}
